'use strict';
var clientControllers = angular.module('clientControllers');

clientControllers.controller('AppCtrl', ['$rootScope', '$scope', '$location', '$routeParams', 'Util', 'DomUtil', 'ThemeUtil', 'App',
    function ($rootScope, $scope, $location, $routeParams, Util, DomUtil, ThemeUtil, App) {

        var appId = $routeParams.appId;
        var subId = $routeParams.subId

        var json = App.get({appId: appId}, (function() {
            $rootScope.app = json.data.app;
            $rootScope.subs = json.data.subs;
            $scope.gotoSub(subId);

        }));
        $scope.showComp = function(sub) {
            $scope.gotoSub(sub.id);
            $scope.$broadcast(sub.code + '_' + 'index', {});
        };
        $scope.showMenu = function() {
            $scope.menuShow = !$scope.menuShow;
        };
        $scope.gotoSub = function(subId) {
            if (subId) {
                $rootScope.CurrentSub = Util.findInArray($rootScope.subs, subId);
            } else {
                $rootScope.CurrentSub = $rootScope.subs[0];
            }
            var viewPath = '/ext/components/' + $rootScope.CurrentSub.code + '/client/layouts/01/view/';
            $scope.appContent = ThemeUtil.themeMainView($rootScope.app.theme);
            $scope.subContent = viewPath + 'main.html';


            $scope.menuShow = false;
        };
}]);